﻿var page = {

    CircleTimer: 0,

    jsonData: {
        PM10: 0,            //PM10浓度 单位：μg/m3
        val_LUX: 0,         //光照浓度 单位：lux
        val_Temperature: 0, //温度 单位：℃
        val_Humidity: 0,    //湿度 单位：%
    },

    /* 此方法在第一次显示窗体前发生 */
    onLoad: function (event) {
        console.log("======================> This is Home of UART");
    },

    /* 此方法展示窗体后发生 */
    onResume: function (event) {

    },

    /* 当前页状态变化为显示时触发 */
    onShow: function (event) {
        currentData = this;         //app.js 中currentData指代当前界面的串口数据
    },

    /* 当前页状态变化为隐藏时触发 */
    onHide: function (event) {

    },

    /* 此方法关闭窗体前发生 */
    onExit: function (event) {
        if (this.CircleTimer != 0) {
            clearInterval(this.CircleTimer);
        }
    },

    //串口0接收回调函数
    uartUpdate: function (event) {
        this.updateUI(event);
    },

    //更新UI函数
    updateUI: function (json_Data) {
        var bufferObj = new Buffer(json_Data, 'utf8');
        var json_str = bufferObj.jsonParse(0, json_Data.length);
        console.dir("======UART DATA======>" + json_str);

        var jsonData = new Object();
        jsonData.val_Temperature = json_str.temperature;
        jsonData.val_Humidity = json_str.humidity;
        jsonData.val_LUX = json_str.lux;
        jsonData.PM10 = json_str.ppm;

        /*刷新各个Label的显示内容*/
        this.setData({
            Temperature: {
                value: jsonData.val_Temperature + "℃",
            },
            Humidity: {
                value: jsonData.val_Humidity + "%",
            },
            PPM: {
                value: jsonData.PM10 + "ppm",
            },
            Top_Temp: {
                value: jsonData.val_Temperature + "℃",
            },
            Top_Hum: {
                value: jsonData.val_Humidity + "%",
            },
            Top_PPM: {
                value: jsonData.PM10 + "ppm",
            }
        })

        this.updateCircle(jsonData);//环形进度条UI
    },

    updateCircle: function (jsonData) {
        var period = 10;
        //计算变化量 
        var interval_1dot0 = (jsonData.val_Temperature - this.jsonData.val_Temperature) / period;//30-25/10=0.5
        var interval_2dot5 = (jsonData.val_Humidity - this.jsonData.val_Humidity) / period;
        var interval_10 = (jsonData.PM10 - this.jsonData.PM10) / period;
        var index = 0;
        var that = this;

        that.CircleTimer = setInterval(function () {
            if (index < period) {
                //更新变化量至UI
                that.setData({
                    CircleProgress1: {
                        value: (that.jsonData.val_Temperature += interval_1dot0)
                    },
                    CircleProgress2: {
                        value: (that.jsonData.val_Humidity += interval_2dot5)
                    },
                    CircleProgress3: {
                        value: (that.jsonData.PM10 += interval_10)
                    }
                });
                /*刷新三个Panel的背景图片*/
                if (that.jsonData.val_Temperature > 30)
                    that.setData({ lv_PM1dot0: { value: "LVbad.png" } });
                else if (jsonData.val_Temperature < 15)
                    that.setData({ lv_PM1dot0: { value: "LVmiddle.png" } });
                else
                    that.setData({ lv_PM1dot0: { value: "LVgood.png" } });

                if (that.jsonData.val_Humidity > 80)
                    that.setData({ lv_PM2dot5: { value: "LVbad.png" } });
                else if (jsonData.val_Humidity > 65)
                    that.setData({ lv_PM2dot5: { value: "LVmiddle.png" } });
                else
                    that.setData({ lv_PM2dot5: { value: "LVgood.png" } });

                if (jsonData.PM10 > 70)
                    that.setData({ lv_PM10: { value: "LVbad.png" } });
                else if (jsonData.PM10 > 55)
                    that.setData({ lv_PM10: { value: "LVmiddle.png" } });
                else
                    that.setData({ lv_PM10: { value: "LVgood.png" } });

            } else {
                clearInterval(that.CircleTimer);
            }
            index++;
        }, 800 / period);
    },

    onBtn: function (event) {
        pm.navigateBack();
    },
};

Page(page);

page = 0;
